package com.ch.tools.server.simulator;


import com.ch.ms.server.login.MapleLoginServer;
import com.ch.tools.server.MapleServer;
import com.ch.tools.server.MapleServerType;
import de.felixroske.jfxsupport.FXMLController;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.util.ResourceBundle;

@FXMLController
@Slf4j
public class MapleServerManagerController implements Initializable {


    @FXML
    private Button startLoginServerButton;


    @FXML
    private TableView<MapleServer> mapleServerTable;

    @FXML
    private TableColumn<MapleServer, String> mapleServerNameColumn;

    @FXML
    private TableColumn<MapleServer, Boolean> mapleServerStatusColumn;


    @FXML
    private TableColumn operationColumn;

    @Autowired
    private MapleLoginServer mapleLoginServer;


    @Override
    public void initialize(URL location, ResourceBundle resources) {

        MapleServer loginServer = new MapleServer(MapleServerType.LOGIN, "登录服务器", "localhost", 9999);
        ObservableList<MapleServer> mapleServers = FXCollections.observableArrayList(loginServer);

        mapleServerNameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        mapleServerStatusColumn.setCellFactory((col) -> {
            TableCell<MapleServer, Boolean> cell = new TableCell<MapleServer, Boolean>() {

                @Override
                public void updateItem(Boolean item, boolean empty) {
                    super.updateItem(item, empty);
                    this.setText(null);
                    this.setGraphic(null);

                    if (!empty) {
                        if (this.getTableView().getItems().get(this.getIndex()).isRunning()) {
                            this.setText("运行中");
                            this.setBackground(new Background(new BackgroundFill(Color.GREEN, CornerRadii.EMPTY, Insets.EMPTY)));
                        } else {
                            this.setText("未运行");
                            this.setBackground(new Background(new BackgroundFill(Color.GRAY, CornerRadii.EMPTY, Insets.EMPTY)));
                        }
                    }
                }

            };
            return cell;
        });

        operationColumn.setCellFactory((col) -> {
            TableCell<MapleServer, Boolean> cell = new TableCell<MapleServer, Boolean>() {

                @Override
                public void updateItem(Boolean item, boolean empty) {
                    super.updateItem(item, empty);
                    this.setText(null);
                    this.setGraphic(null);

                    if (!empty) {
                        MapleServer server = this.getTableView().getItems().get(this.getIndex());
                        Button startBtn = new Button("启动");
                        startBtn.setDisable(server.isRunning());
                        startBtn.setOnMouseClicked(event -> {
                            // loginServer.setRunning(true);
                            //  System.out.println(this.getTableView().getItems().get(this.getIndex()).isRunning());


                            switch (server.getType()) {

                                case LOGIN:
                                    server.setRunning(mapleLoginServer.bind(server.getHost(), server.getPort()));
                                    break;
                            }
                            mapleServerTable.refresh();
                        });

                        Button stopBtn = new Button("关闭");
                        stopBtn.setDisable(!server.isRunning());
                        stopBtn.setOnMouseClicked(event -> {
                            server.setRunning(!mapleLoginServer.shutdown());
                            mapleServerTable.refresh();
                        });
                        this.setGraphic(new HBox(startBtn, stopBtn));
                    }
                }

            };
            return cell;
        });

        mapleServerTable.setItems(mapleServers);

    }
}
